<template>
  <div>
    <button @click="modalOpen = true">弹出一个模态框</button>

    <teleport to="body">
      <div v-if="modalOpen" class="modal">
        <div>
          这是一个弹框，挂载在 body 元素上。
          <button @click="modalOpen = false">关闭</button>
        </div>
      </div>
    </teleport>

  </div>
</template>

<script lang="ts">
import { defineComponent, ref } from "vue";

export default defineComponent({
  setup() {
    const modalOpen = ref(false);

    return { modalOpen };
  },
});
</script>

<style scoped>
.modal {
  position: absolute;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  background-color: rgba(0, 0, 0, 0.5);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
}
.modal div {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: space-around;
  background-color: white;
  width: 350px;
  height: 300px;
  padding: 5px;
}
</style>
